function struTwo=structureMerge(struOne,struTwo,flagReplace) 
% function struTwo=structureMerge(struOne,struTwo,flagReplace) 
% Merge the contents of the first structure, *struOne* into the second 
% structure, *struTwo*. 
% If flagReplace==1 then will re-write existing fields, 
% else will produce an error. Default is flagReplace=0; 
% Produces an error if there are repetitions. 
% AJ 1/17/2012
if nargin < 3 || isempty(flagReplace)==true 
    flagReplace=0; 
end 
nameOne=fieldnames(struOne); 
nameTwo=fieldnames(struTwo); 
NOne=length(nameOne); 
for ii=1:NOne;
    if any( strcmp(nameTwo,nameOne{ii}) ~= 0);
        if flagReplace==0
            dispaj('Error ',nameOne{ii},' already exists in second structure');
            error(['Error ',nameOne{ii},' already exists in second structure']);
        else
            dispaj('Warning! Overwriting ',nameOne{ii},' in second structure');
            warning(['Overwriting ',nameOne{ii},' in second structure']);                        
        end
    end
    struTwo=setfield(struTwo,nameOne{ii},(getfield(struOne,nameOne{ii})));
end 